package com.fit.piris.dao.user.fanpage;

import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.piris.entities.fanpage.FanPage;
import com.fit.piris.entities.user.fanpage.UserFanPage;

public class UserFanPageDAOImpl implements UserFanPageDAO {

	@Inject
	private Session session;
	
	public boolean save(UserFanPage userFanPage) {
		if (userFanPage!=null) {
			session.save(userFanPage);
			return true;
		}
		return false;
	}

	public boolean delete(UserFanPage userFanPage) {
		if (userFanPage!=null) {
			session.delete(userFanPage);
			return true;
		}
		return false;
	}

	public boolean update(UserFanPage userFanPage) {
		if (userFanPage!=null) {
			session.update(userFanPage);
			return true;
		}
		return false;
	}

	public boolean deleteByID(Long idUser, Long idFanPage) {
		UserFanPage u = findByID(idUser, idFanPage);
		if (u!=null) {
			session.delete(u);
			return true;
		}
		return false;
	}

	public UserFanPage findByID(Long idUser, Long idFanPage) {
		List<UserFanPage> u = session.createCriteria(UserFanPage.class)
				.add(Restrictions.eq("pk.user.id", idUser))
				.add(Restrictions.eq("pk.fanPage.id", idFanPage))
				.list();
		if (u!=null) {
			if (u.size()>0) {
				return u.get(0);
			}
		}
		return null;
	}

	public boolean isLiked(Long idUser, Long idFanPage) {
		if (findByID(idUser, idFanPage)!=null) {
			return true;
		}
		return false;
	}

	public List<UserFanPage> findAllForUser(Long idUser) {
		return session.createCriteria(UserFanPage.class)
				.add(Restrictions.eq("pk.user.id", idUser))
				.list();
	}

}
